package components.dialogs
{
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_list;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import UIUtils.uif_util;
	
	import classes.Commands.c_linkCommand;
	import classes.Commands.c_visibilityCommand;
	import classes.c_blendMode;
	import classes.c_events;
	
	import components.c_checkboxLabel;
	import components.editor.c_baseEditor;
	import components.editor.c_baseElement;
	import components.propertyCtrls.c_checkboxList;
	
	import flash.events.Event;
	
	import org.hamcrest.collection.array;
	
	public class c_layersDialog extends c_childListDialog {
		private var _editor				: c_baseEditor;
		private var _externalChange		: Boolean;
		public function c_layersDialog(parent:uif_component, a_editor: c_baseEditor, a_title:String='') {
			_editor = a_editor;
			super(parent, a_editor.imageLayer, a_title);
			_externalChange = false;
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			_editor.imageLayer.forEach(elementEventListener, c_baseElement);
			
			addEvent(c_events.CHANGECURRENTELEMENT, doChangeCurrentElement, cl.workspace);
			addEvent(c_events.ADDEDLAYER, doAddedLayer, cl.workspace);
			addEvent(Event.REMOVED, doRemovedElement, stage);
		}
		
		protected function elementEventListener(elem: c_baseElement): void {
			addEvent(uif_component.CHANGEVISIBLE, doChangeVisibleElement, elem);
			addEvent(uif_ecomponent.CHANGEINDEX, doChangeElementIndex, elem);
		}
		
		protected function doChangeCurrentElement(e: ObjectEvent): void {
			if (e.object) {
				if ((listLayers.selectIndex == -1) || (e.object.name != listLayers.selectItem._data.name)) {
					listLayers.list.forEach(function(item: Object, i: int, arr: Array): void {
						if (item.name == e.object.name)
							listLayers.selectIndex = i;
					});
				}
				var a_data: Object = values[1];
				a_data.value = (e.object as c_baseElement).blendMode;
				values[1] = a_data;
			} else listLayers.selectIndex = -1;
		}
		
		protected function doRemovedElement(e: Event): void {
			if (e.target is c_baseElement) {
				var vl: Array = valuesList;
				for (var i:int=0; i<vl.length; i++)
					if (vl[i].name == e.target.name) {
						vl.splice(i, 1);
						break;
					}
				propertyCtrls[0].control._data = uif_util.clone(values[0]);
			}
		}
		
		protected function doAddedLayer(e: ObjectEvent): void {
			elementEventListener(e.object as c_baseElement);
			updateElementList();
		}
		
		protected function doChangeElementIndex(e: Event): void {
			updateElementList();
		}
		
		protected function doChangeVisibleElement(e: Event): void {
			_externalChange = true;
			getListItemFromName(e.target.name).checkbox.checked = e.target.visible;
			_externalChange = false;
		}
		
		override protected function doChange(e:Event):void {
			if (!_externalChange) {
				var list: Array = valuesList;
				list.forEach(function(item: Object, i: int, arr: Array): void {
					var elem: c_baseElement = _editor.imageLayer.getChildByName(item.name) as c_baseElement;
					if (elem && (elem.visible != item.checked)) {
						executeCommand(new c_visibilityCommand(elem, item.checked));
					}
				});
				
				if ((values[0].value > -1) && (values[0].value < list.length)) {
					var select: c_baseElement = _editor.imageLayer.getChildByName(list[values[0].value].name) as c_baseElement;
					if (select != _editor.dragRect.link) {
						executeCommand(new c_linkCommand(_editor.dragRect, select.visible?select:null));
					}
				}
				
				updateBlendMode();
			}
		}
		
		protected function updateBlendMode(): void {
			var ce: c_baseElement = curElement;
			var bm: String = values[1].value.value;
			if (ce && (ce.blendMode != bm)) {
				ce.blendMode = bm;
			}				
		}
	}
}